﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using BYFW.DAL;
using BYFW.BOL;
using System.Data;

namespace LDSP.UI.EnterpriseAdmin
{
    public partial class MngEnterpriseUser : BRL.BaseEnterpriseAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    this.InitDataDynamicBefore();
                    this.InitData();
                    this.GetCondition();
                    this.BindGrid();
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        /// <summary>
        /// 传入参数，设置过滤条件
        /// </summary>
        protected void SetSpecialPageInfo()
        {
            g_pageInfo.Direction = true;
            g_pageInfo.Sort = "code";
            g_pageInfo.PageSize = 10;
            g_pageInfo.PageIndex = this.grd_list.PageIndex;
        }

        protected void InitData()
        {
            this.SetSpecialPageInfo();
        }
        protected void InitDataDynamicBefore()
        {
        }
        protected void BindGrid()
        {
            DataTable dt = BYFW.SFL.CommonOP.SortedTable(new EnterpriseUser().GetRecords(g_pageInfo.ConditionWhere, true), g_pageInfo.GetSortExpression());

            this.grd_list.PageSize = g_pageInfo.PageSize;
            this.grd_list.PageIndex = g_pageInfo.PageIndex;
            this.grd_list.DataSource = dt;
            this.grd_list.DataBind();

            this.TotalRecords.Text = LDSP.Helper.StringUtil.GetSearchText(dt.Rows.Count); ;
        }

        protected void GetCondition()
        {
            string cond = "";
            if (this.txt_name.Text.Trim() != "")
            {
                cond += DefaultDB.GetSQLMultiCondition("code", EDataType.String, this.txt_name.Text.Trim(), true, true);
            }

            cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("enterpriseID", EDataType.Int, this.LoginUser.EnterpriseID.ToString(), "=", true), true);
            cond = this.AppendCondition(cond, new BaseEntity().DB.GetSQLNormalCondition("userType", EDataType.Int, "1", "=", true), true);//"1" 是普通管理员。

            g_pageInfo.Condition = cond;
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                this.JSRedirect("AddEnterpriseUser.aspx", false);
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_delete_Click(object sender, EventArgs e)
        {
            try
            {
                this.DeleteCheckedInGrid<EnterpriseUser>(this.grd_list);
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_search_Click(object sender, EventArgs e)
        {
            try
            {
                this.GetCondition();
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected void grd_list_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int id = this.GetInt(e.CommandArgument.ToString());
                if (e.CommandName == "updateItem")
                {
                    this.JSRedirect("AddEnterpriseUser.aspx?id=" + id, false);
                }
                else if (e.CommandName == "deleteItem")
                {
                    EnterpriseUser ett = new EnterpriseUser();
                    ett.GetProperties(id);
                    ett.Delete();

                    this.BindGrid();
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    this.ShowSequenceNumber(e.Row, 1);
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                g_pageInfo.PageIndex = e.NewPageIndex;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                if (g_pageInfo.Sort == e.SortExpression)
                {
                    g_pageInfo.Direction = !g_pageInfo.Direction;
                }
                else
                {
                    g_pageInfo.Direction = true;
                }
                g_pageInfo.Sort = e.SortExpression;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
    }
}